package com.DosonLeung.homeschoolcommunication.Utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.DosonLeung.homeschoolcommunication.Controllers.ChatActivity;
import com.DosonLeung.homeschoolcommunication.Controllers.MainActivity;
import com.DosonLeung.homeschoolcommunication.R;

/**
 * Created by DosonLeung on 2014/11/29.
 */
public class NotificationUtil {
    //创建通知
    public static void CreateInform(Context context,String activity,String alert, String title, String detail, int no_id) {
        //定义一个PendingIntent，当用户点击通知时，跳转到某个Activity(也可以发送广播等)
        Intent intent;
        if(activity.equals("ChatActivity"))
            intent = new Intent(context, ChatActivity.class);
        else
            intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//关键的一步，设置启动模式
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        //创建一个通知
        Notification notification = new Notification(R.drawable.icon, alert, System.currentTimeMillis());
        notification.setLatestEventInfo(context, title, detail, pendingIntent);
        notification.flags = Notification.FLAG_AUTO_CANCEL;

        //用NotificationManager的notify方法通知用户生成标题栏消息通知
        NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(no_id, notification);//id是应用中通知的唯一标识
        //如果拥有相同id的通知已经被提交而且没有被移除，该方法会用更新的信息来替换之前的通知。
    }
}
